#include "cell.hpp"
#include "configs.hpp"

namespace CellUtil{
  void read_bits_from_cell(vec_GF2& bits, const Cells& cells){
    bits.kill();
    for(size_t i = 0; i < cells.size(); ++i)
      append(bits, *(cells[i] -> bits));
  }

  // compute the levels of the cells according to the codeword
  void compute_cell_levels(vector<int>& levels, const vec_GF2& codeword){
    int level;
    int exp;
    if ((codeword.length() % 2) != 0){
      cout << "Error: the length of the codeword should be even" << endl;
      exit(EXIT_FAILURE);
    }
    
    for(long i = 1; i <= codeword.length(); i += BITS_PER_CELL) {
      level = 1;
      for(long j = 0; j < BITS_PER_CELL; j++){
	//      cout << "BIT:" << codeword(i+j) << endl;
	if (IsOne(codeword(i+j))){
	  exp = BITS_PER_CELL-j-1;
	  level += pow((double) 2, exp);
	}
      }
      //    cout << "Level:" << level << endl;
      levels.push_back(level);
    }
  }
  
  double compute_bit_error_rate(const vector< Cell* >& cells, const long& n){
    long errnum = 0;
    for(long i = 0; i < (long) cells.size(); i++){
      Cell* cel = cells[i];
      for(long j = 1; j <= BITS_PER_CELL; j++){
	if ((*(cel -> bits))(j) != (*(cel -> bits0))(j))
	  errnum++;
      }
    }
    return ((double) errnum / (double) n);
  }
  

}
